/**
 * 
 */
package org.jaxson.struts.encoder;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.Serializable;

import flexjson.JSONSerializer;


/**
 * Encodes a Serializable model to a JSON string.
 * 
 * @author Joe Maisel
 * 
 */
public class JsonEncoder extends ResponseEncoder
{

	private static final Log lager = LogFactory.getLog( JsonEncoder.class.getName() );


	/**
	 * @throws EncodingException
	 * @see org.jaxson.struts.ResponseEncoder#encode(java.io.Serializable)
	 */
	@Override
	public void encode( Serializable model ) throws EncodingException
	{
		try
		{
			JSONSerializer s = new JSONSerializer();
			// String json = "{\"result\": { " + s.deepSerialize (model) + "}}";
			String json = model == null ? "" : s.deepSerialize( model ).trim();
			lager.debug( "writing json: " + json );
			write( json.getBytes() );
		}
		catch( Exception e )
		{
			e.printStackTrace();
			throw new EncodingException();
		}
	}
}
